<template>
  <div>
    <!-- 添加或修改对话框 -->
    <el-dialog ref="editDialog" v-model="openFlag" title="申请" width="50%" append-to-body>
      <el-form ref="form" :model="formData" label-width="100" :rules="rules">
        <el-row>
          <el-col :span="12">
            <el-form-item label="转入公司" prop="companyId">
              <company-select v-model="formData.companyId" v-model:name="formData.companyName"></company-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="转入管理员" prop="adminId">
              <user-select v-model="formData.adminId" v-model:name="formData.adminName"></user-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-form label-width="100">
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input type="textarea" v-model="applyData.remark" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <choose-table ref="chooseTable"></choose-table>

      <div slot="footer" class="dialog-footer">
        <el-row type="flex" justify="end">
          <el-button type="primary" @click="save">保存</el-button>
          <el-button type="primary" @click="sub">提交</el-button>
          <el-button @click="close">取 消</el-button>
        </el-row>
      </div>
    </el-dialog>

  </div>
</template>

<script setup>

import api from "./api.js";
import CompanySelect from "@/components/CommonSelect/CompanySelect.vue";
import UserSelect from "@/components/User/UserSelect.vue";
import ChooseTable from "@/components/Asset/ChooseTable.vue";

const emit = defineEmits(['sub']);

const form = ref();
const chooseTable = ref()

const props = defineProps({
  applyType: {
    type: String,
    required: true,
  }
})

// 是否编辑
const isEdit = ref(false);

const applyDataInit = {
  id: null,
  applyType: props.applyType,
  remark: null,
}

const applyData = reactive({ ...applyDataInit });

// 表单初始数据
const formDataInit = {
  companyId: null,
  companyName: null,
  adminId: null,
  adminName: null,
  remark: null,
}

const openFlag = ref(false);

// 表单数据
const formData = reactive({...formDataInit});

// 表单规则
const rules = {
  targetCompanyId: {
    required: true,
    message: '不能为空',
    trigger: 'blur',
  },
  adminId: {
    required: true,
    message: '不能为空',
    trigger: 'blur',
  }
}

// 重置
function reset() {
  Object.assign(applyData, applyDataInit);
  Object.assign(formData, formDataInit);
}

// 打开窗口
function open(row) {
  openFlag.value = true;

  reset();
  if (row) {
    api.detail(row).then(response=> {
      Object.assign(applyData, response);
      const resFormData = response.formData;
      if (resFormData) {
        Object.assign(formData, resFormData);
      }
      let relItems = formData.items;
      if (!relItems) {
        relItems = []
      }
      chooseTable.value.setList(relItems)
    })
    isEdit.value = true;
  } else {
    isEdit.value = false;
  }
}

function close() {
  openFlag.value = false;
}

function getParams() {
  const table = chooseTable.value;
  const apply = { ...applyData };
  formData.items = table.getList();
  apply.formData = JSON.stringify(formData);
  return {
    edit: isEdit.value,
    apply: apply
  }
}

function check() {
  return new Promise((resolve, reject) => {
    form.value.validate(async (valid) => {
      if (valid) {
        resolve();
      } else {
        reject();
      }
    });
  })
}

/** 保存 */
function save() {
  check().then(()=> {
    api.save(getParams()).then(response => {
      emit("sub");
      close();
    });
  });
}

function sub() {
  check().then(()=> {
    api.saveAndSub(getParams()).then(response => {
      emit("sub");
      close();
    });
  });
}

defineExpose({
  open
})

</script>

